package io.renren.newpaypay;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;



public class PayGateway {
	

	
/*

	public static void main(String[] args){
		testPayGetway();
    }
	
	public static void testPayGetway(){

		//提交参数设置
		PayGateway payGateway = new PayGateway();
		payGateway.merId = "62117401";  //62010002给下面商户测试
		payGateway.orderId =  "bzyg9dvsimaboehh";




		//支付提交
		String url =  "http://47.106.242.226:7000/sdk/json.do";
		String key = "7461b450-e7a8-4993-97ed-5eb33e051222";;
		payGateway.post(url,key);

	}
	
	public String post(String url,String key){

		try {
			packData();
			tradeId = "payOrderQuery";
			tradeData = jsonObj.toString();
			String md5Src = tradeData + "&" + key;  //md5源串

			//加密前：{"merId":"62117401","orderId":"1586863620755","goods":"巧克力100136","amount":"0.03","expTime":"","notifyUrl":"http://xxx","pageUrl":"http://xxx","reserve":"保留123","extendInfo":"扩展信息","payMode":"01","payBankId":"CCB","creditType":"2","userId":"000001","clientIp":"127.0.0.1"}&7461b450-e7a8-4993-97ed-5eb33e051222
			byte[] bTradeSign = Md5.md5(md5Src.getBytes("UTF-8")); //md5加密
			tradeSign = ByteUtil.bytes2HexStr(bTradeSign);//字节转字符,如0x1234AB -> “1234AB”

			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("tradeId",tradeId));
			nvps.add(new BasicNameValuePair("ver","1.0"));
			nvps.add(new BasicNameValuePair("tradeData",tradeData));
			nvps.add(new BasicNameValuePair("tradeSign",tradeSign));
			

			String response = HttpClientUtils.doPost(url,nvps,HTTP.UTF_8).trim();
			System.out.println("response: " + response);
//{"backData":{"merId":"62117401","orderId":"bzyg9dvsimaboehh","amount":"196.00","result":"U","resultInfo":"结果不确定","payTime":"2020-04-15 15:19:58","reserve":"","payOrderId":"5202142","bankJournal":"","creditType":"","otherPay":""},"backSign":"A5CDBA7A53A2FE0922D55ED6E4268660","info":"交易成功","status":"0000"}

//			response: {"backData":{"payUrl":"http://client.newpingon.com/c/qr.do?a=0D5F07817B704C4061A87B5CF2FA4969"},"backSign":"BC6BF01975EB78A12965839EE4E09CAA","info":"交易成功","status":"0000"}
//			md5Src: {"payUrl":"http://client.newpingon.com/c/qr.do?a=0D5F07817B704C4061A87B5CF2FA4969"}&7461b450-e7a8-4993-97ed-5eb33e051222

			//返回数据处理
			JSONObject jsonResp = JSONObject.fromObject(response);
			String backSign = jsonResp.getString("backSign");
			if((backSign == null)||(backSign.length() == 0)){
				return "";
			}
			
			//有数据
			JSONObject backData = jsonResp.getJSONObject("backData");
			md5Src = backData + "&" + key;
			System.out.println("md5Src: " + md5Src);
			boolean check = Md5.md5Verify(md5Src, backSign);
			if(!check){
				return "";
			}
			return backData.getString("payUrl");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public void packData(){
		addParam("merId",merId);
		addParam("orderId",orderId);
		addParam("goods",goods);
		addParam("amount",amount);
		addParam("expTime",expTime);
		addParam("notifyUrl",notifyUrl);
		addParam("pageUrl",pageUrl);
		addParam("reserve",reserve);
		addParam("extendInfo",extendInfo);
		addParam("payMode",payMode);
		addParam("payBankId",payBankId);
		addParam("creditType",creditType);
		addParam("userId",userId);
		addParam("clientIp",clientIp);
	}
	
	protected void addParam(String name, String value) {
		jsonObj.put(name, value);
	}
	

	*//**
	 * Runtime.getRuntime().exec("cmd   /c   start   http://bbs.csdn.net/topics/390157559 ");
	 *	Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
	 * @param strHtml
	 *//*
	public void showInBrowser(String filePath,String strHtml){
		try {
        	File file = new File(filePath);
        	//PrintStream ps = new PrintStream(new FileOutputStream(file)); 
        	OutputStreamWriter ps = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
            ps.write(strHtml);// 往文件里写入字符串
            ps.flush();
            ps.close();
            Runtime ce=Runtime.getRuntime();
            String strCmd = String.format("cmd   /c   start  %s",file.getAbsolutePath());
            System.out.println(strCmd);
			ce.exec(strCmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String tradeId;
	public String ver;
	public String tradeData;
	public String tradeSign;
	
	public String merId;
	public String orderId;
	public String goods;
	public String amount;
	public String expTime;
	public String notifyUrl;
	public String pageUrl;
	public String reserve;
	public String extendInfo;
	public String payMode;
	public String payBankId;
	public String creditType;
	public String userId;
	public String clientIp;*/
	
}


